﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Motoball.screenmanager;
using Microsoft.Xna.Framework.Input;
using System.Xml;
using Microsoft.Xna.Framework.Storage;


namespace Motoball.Paddles
{
    public class Crusher : PaddleContent
    {
        public Crusher(Paddle paddle)
            : base(paddle)
        {
            animation = new CrusherAnimation();
            paddleHeight = 244;
            paddleWidth = 516;
            maxPaddleDistance = 660;//440;
            ballHeight = 2280;
            weight = 1.1f;
            type = "crusher";
        }

        public override void Initialize(float innerRadius)
        {
            shield = new BoneProperties[1];
            shield[0] = new BoneProperties(paddle.basicModel.model.Bones["Chassis_Schild"]);

            base.Initialize(innerRadius);
        }
        public static float specialWeightFactor = 2f;
        public override void SpecialMovement(bool action)
        {
            base.SpecialMovement(action);
            if (action)
            {
                weight *= specialWeightFactor;
                AudioManager.PlayCue("crusher_0");
            }
            else
                weight = defaultWeight;
           
            
        }

        public override bool Update(GameTime gameTime)
        {
            if (!base.Update(gameTime))
            {
                if (specialMoveTimer < 0.4f || specialMoveTimer > 2.6f)
                {
                    float temp = specialMoveTimer;
                    if (specialMoveTimer > 2.6f)
                        temp = 3f - temp;
                    bloomValue = MathHelper.Lerp(1f, 30f, temp * 2.5f);
                }
                set.SetBloomValues(bloomValue);

                specialMoveTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (specialMoveTimer >= 3f)
                {
                    SpecialMovement(false);
                    specialMoveTimer = 0f;
                }
                return false;
            }
            return true;
        }


        //public override void Update(GameTime gameTime)
        //{
        //    base.Update(gameTime);
        //    if (GameScreen.screenManager.Input.IsKeyPress(Keys.U))
        //        paddleHeight += 1f;
        //    if (GameScreen.screenManager.Input.IsKeyPress(Keys.J))
        //        paddleHeight -= 1f;
        //    if (GameScreen.screenManager.Input.IsKeyPress(Keys.H))
        //        paddleWidth += 1f;
        //    if (GameScreen.screenManager.Input.IsKeyPress(Keys.K))
        //        paddleWidth -= 1f;
        //    paddle.Recalc();
        //}
    }

    public class CrusherAnimation : PaddleAnimation
    {
        double[] timer;
        public CrusherAnimation()
            : base()
        {
            timer = new double[2];
            timer[0] = 0.01f;
            timer[1] = 0.03f;
        }
        BoneProperties exhaustLeft;
        BoneProperties exhaustRight;
        BoneProperties[] motor;
        public override void Initialize(float innerRadius, Model model)
        {
            #if !XBOX
            save.fileName = "crusher";
#endif
            base.Initialize(innerRadius, model);
            leftWheel = model.Bones["Rad1_Felge"];
            rightWheel = model.Bones["Rad2_Felge"];
            leftWheelTransform = leftWheel.Transform;
            rightWheelTransform = rightWheel.Transform;

            exhaustLeft = new BoneProperties(model.Bones["Auspuff1"]);
            exhaustRight = new BoneProperties(model.Bones["Auspuff2"]);
            motor = new BoneProperties[4];
            motor[0] = new BoneProperties(model.Bones["Chassis_Teil1"]);
            motor[1] = new BoneProperties(model.Bones["Chassis_Teil2"]);
            motor[2] = new BoneProperties(model.Bones["Chassis_Teil12"]);
            motor[3] = new BoneProperties(model.Bones["Chassis_Teil13"]);
            

            nextPos = new Vector3[2];
            lastPos = new Vector3[2];
            posIndex = new int[2];
            posIndex[0] = 1;
            posIndex[1] = 4;
            nextPos[0] = exhaustKeyFrames[posIndex[0]] / 10f;
            nextPos[1] = exhaustKeyFrames[posIndex[1]] / 10f;
            lastPos[0] = exhaustKeyFrames[posIndex[0] - 1] / 10f;
            lastPos[1] = exhaustKeyFrames[posIndex[1] - 1] / 10f;
        }

        public override void UpdateColor(Vector3 color, BasicModel basicModel)
        {
            //basicModel.diffColors["Auspuff1"] = Vector3.Lerp(Vector3.Zero, color, 0.8f);
            //basicModel.diffColors["Auspuff2"] = Vector3.Lerp(Vector3.Zero, color, 0.8f);

            basicModel.diffColors["Chassis_Schild"] = Vector3.Lerp(Vector3.One, color, 0.7f);

            ////engine
            //basicModel.diffColors["Obj_000001"] = Vector3.Lerp(Vector3.One, color, 0.5f);
            ////left wheel-protector
            //basicModel.diffColors["Obj_000003"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);
            ////right wheel-protector
            //basicModel.diffColors["Obj_000004"] = Vector3.Lerp(Vector3.Zero, color, 0.4f);
        }
        
        Vector3[] nextPos;
        Vector3[] lastPos;
        int[] posIndex;
        public override void UpdateAnimation(GameTime gameTime)
        {
            for (int i = 0; i < 2; i++)
            {
                timer[i] += gameTime.ElapsedGameTime.TotalSeconds;
                //if defined time has passed - create a new random-direction exhaust is shaking towards
                if (timer[i] > 0.03f)
                {
                    timer[i] -= 0.03f;
                    posIndex[i] = (posIndex[i] + 1) % 10;
                    lastPos[i] = nextPos[i];
                    nextPos[i] = exhaustKeyFrames[posIndex[i]] / 30f;

                    //randomPos[i] = new Vector3(
                    //    (float)random.NextDouble(),
                    //    (float)random.NextDouble(),
                    //    (float)random.NextDouble()) * 3;
                }
            }
            exhaustLeft.modelBone.Transform = Matrix.CreateTranslation(
                Vector3.Lerp(lastPos[0],
                    nextPos[0],
                    (float)Math.Sin(timer[0])))
                * exhaustLeft.sourceTransform;
            exhaustRight.modelBone.Transform = Matrix.CreateTranslation(
                Vector3.Lerp(lastPos[1],
                nextPos[1],
                (float)Math.Sin(timer[1])))
                * exhaustRight.sourceTransform;
            for (int i = 0; i < 4; i++)
            {
                motor[i].modelBone.Transform = Matrix.CreateTranslation(
                    Vector3.Lerp(lastPos[0],
                    nextPos[0],
                    (float)Math.Sin(timer[1])))
                    * motor[i].sourceTransform;
            }
            
            base.UpdateAnimation(gameTime);
        }
    }
}
